<?php
namespace app\business\controller;
use app\business\model\BusinessLessen;
use app\business\model\BusinessLogin;
use think\Db;
use think\Session;

class Lessen extends Base
{
    public function index()
    {
        $username = Session::get('username');

        $count = BusinessLogin::where('username',$username)->find();

        $num=$count['business_id']; //商户id

        $data = BusinessLessen::where('business_id',$num)->select();

        $this->assign('data',$data);

        return $this->fetch();
    }

    public function create()
    {
        return $this->fetch();
    }

    public function store()
    {
        $username = Session::get('username');

        $count = BusinessLogin::where('username',$username)->find();

        $num=$count['business_id']; //添加优惠券的商户

        $_POST['business_id'] = $num;

        if($_POST['lessen_full']<=$_POST['lessen_reduce']){
            return $this->error('减少的金额不能大于享受优惠的金额');
        }
        if(request()->isPost()) {
            $file = request()->file("lessen_image");
            if ($file) {  //如果有上传图片
                $info = $file->move(ROOT_PATH . 'public/uploads/shop');
                $nowDate = date('Ymd');
                if ($info) {
                    $path = $nowDate . '/' . $info->getFilename();
                } else {
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            }
        }
        $_POST['lessen_image'] = $path;

        $_POST['lessen_date'] = strtotime($_POST['lessen_date']); // 返回 “f”

        BusinessLessen::create($_POST);

        return redirect(url("/business/lessen/index"));
    }

    public function delete($id)
    {
        BusinessLessen::where('id',$id)->delete();

        return redirect(url("/business/lessen/index"));
    }

    public function edit($id)
    {
        $data = BusinessLessen::where('id',$id)->find();

        $data['lessen_date'] = date('Y-m-d',$data['lessen_date']);

        $this->assign('id',$id);

        $this->assign('data',$data);

        return $this->fetch();
    }

    public function update()
    {
        $id = $_POST['id'];

        unset($_POST['id']);

        if(request()->isPost()) {
            $file = request()->file("lessen_image");
            if ($file) {  //如果有上传图片
                $info = $file->move(ROOT_PATH . 'public/uploads/shop');
                $nowDate = date('Ymd');
                if ($info) {
                    $path = $nowDate . '/' . $info->getFilename();
                    $_POST['lessen_image'] = $path;
                } else {
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            }else{
                $demo = BusinessLessen::where('id',$id)->find();
                
                $_POST['lessen_image'] = $demo['lessen_image'];
            }
        }



        $_POST['lessen_date'] = strtotime($_POST['lessen_date']);

        BusinessLessen::where('id',$id)->update($_POST);

        return redirect(url("/business/lessen/index"));
    }
}